/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import net.worlds.component.JColorPicker;


public class PropsTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    private int row;
    private int column;
    private Component component;
    private String key;
    public static boolean cellEditing = false;
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        cellEditing = true;
        key = (String) table.getModel().getValueAt(row, 0);
        this.row = row;
        this.column = column;
        component = PropsTableCellComponent.getComponentFor(this, table.getModel().getValueAt(2, 1).toString(), column, key, value);
        if(component instanceof JCheckBox)
        {
            ((JCheckBox)component).addItemListener(new ItemListener()
            {

                public void itemStateChanged(ItemEvent e) {
                    stopCellEditing();
                }

            });
        }
        return component;
    }

    public Object getCellEditorValue() {
        if(component instanceof JCheckBox)
        {
            return ((JCheckBox)component).getSelectedObjects() != null;
        }
        else if(component instanceof JTextField)
        {
            return ((JTextField)component).getText();
        }
        else if(component instanceof JColorPicker)
        {
            return ((JColorPicker)component).getValue();
        }
        else
        {
            throw new RuntimeException("Invaid component type for property editor");
        }
    }
    @Override
    public boolean stopCellEditing()
    {
        cellEditing = false;
        return super.stopCellEditing();
    }
    @Override
    public void cancelCellEditing()
    {
        super.cancelCellEditing();
        cellEditing = false;
    }
}
